let nameEl = document.querySelector('#name')
let emailEl = document.querySelector('#email')
let passwordEl = document.querySelector('#password')
let passwordConfirm = document.querySelector('#password-confirm')
let btnEl = document.querySelector('.btn')


// var showtime = function () {

//     var nowdate = new Date();

//     var year = nowdate.getFullYear(),

//         month = nowdate.getMonth() + 1,

//         date = nowdate.getDate(),

//         day = nowdate.getDay(),

//         week = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],

//         h = nowdate.getHours(),

//         m = nowdate.getMinutes(),

//         s = nowdate.getSeconds(),

//         h = checkTime(h),

//         m = checkTime(m),

//         s = checkTime(s);

//     return year + "年" + month + "月" + date + "日" + week[day] + " " + h + ":" + m + ":" + s;

// }

// var checkTime = function (i) {

//     if (i < 10) {

//         i = "0" + i;

//     }

//     return i;

// }

btnEl.onclick = async function () {
    // 获取数据
    // let createAt = showtime() //时间
    let password = passwordEl.value.trim() //密码
    let rPassword = passwordConfirm.value.trim() //确认密码
    let tel = emailEl.value.trim() //邮箱/手机号码
    let username = nameEl.value.trim() //名字
    //   /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/
    let phone = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/; //号码验证
    let pass = /[a-zA-Z]/ //密码验证

    if (!phone.test(tel)) {
        return alert("手机号不符合要求")
    }

    if (!pass.test(password)) {
        alert('密码必须包含字母')
    }

    // // 数据校验
    if (!password || !rPassword) {
        return alert('账户或密码未填写')
    }

    if (password != rPassword) {
        return alert('密码和确认密码不一致')
    }

    // 发送给服务端
    let res = await axios({
        method: 'POST',
        url: '/api/user/register', // 每个接口的地址都不一样
        data: {
            username,
            password,
            createAt: new Date(),
            tel
        },
    })

    console.log(res);
    // 根据返回内容作出对应响应（失败->提示，成功->跳转至登陆）
    let {
        code,
        msg,
    } = res.data

    // 报错，弹出错误信息
    if (code) {
        return alert(msg)
    }

    // localStorage.token = data.token;
    // 注册成功，跳转至登陆

    location.href = '../../html/dc/login.html'
}